package com.example.baseapp.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.example.baseapp.business.entity.User;
import com.example.baseapp.business.mapper.UserMapper;
import com.example.baseapp.business.service.UserService;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.annotation.Resource;
import java.util.List;

/**
 * 用户表(User)表服务实现类
 *
 * @author zhanglei
 * @since 2022-03-23 15:26:26
 */
@Service("userService")
public class UserServiceImpl implements UserService {
    @Resource
    private UserMapper userMapper;

    @Override
    public boolean existUser(User user) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = getUserLambdaQueryWrapper(user);
        return userMapper.selectList(lambdaQueryWrapper) != null;
    }

    @Override
    public List<User> findByUserNameOrEmailOrTel(User user) {
        LambdaQueryWrapper<User> lambdaQueryWrapper = getUserLambdaQueryWrapper(user);
        return userMapper.selectList(lambdaQueryWrapper);
    }

    private LambdaQueryWrapper<User> getUserLambdaQueryWrapper(User user) {
        return new LambdaQueryWrapper<User>()
                .eq(user.getId() != null,User::getId, user.getId())
                .eq(StringUtils.hasText(user.getName()),User::getName,user.getName())
                .eq(StringUtils.hasText(user.getEmail()),User::getEmail,user.getEmail())
                .eq(StringUtils.hasText(user.getPasswordDigest()),User::getPasswordDigest,user.getPasswordDigest())
                .eq(StringUtils.hasText(user.getTel()),User::getTel,user.getTel());
    }
}